package primetest.impl.dao;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import primetest.dto.TermDto;
import primetest.impl.dao.TermDao;
import primetest.impl.dao.Transaction;


//@Stateless
public class TermDao implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 1332526014429976523L;
	/**
	 *
	 */


	private final Logger logger = LoggerFactory.getLogger(this.getClass());


	@Inject
	public TermDao(){
	}

	public List<TermDto> getAllList(Transaction transaction) throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(GET_ALL);
			resultSet = prepareStatement.executeQuery();

			List<TermDto> allItems = new ArrayList<TermDto>();
			while (resultSet.next()) {
				TermDto termDto = makeTermDto(transaction, resultSet);
				allItems.add(termDto);
			}
			return allItems;
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}
	}

	private TermDto makeTermDto(Transaction transaction, ResultSet resultSet) throws SQLException, IOException {
		TermDto termDto = new TermDto(resultSet.getString(1), resultSet.getString(2)
				);
		return termDto;
	}

	public void addTerm(Transaction transaction, TermDto termDto) throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;

		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(INSERT_ONE);
			prepareStatement.setString(1, termDto.getKey());
			prepareStatement.setString(2, termDto.getKey());
			prepareStatement.executeUpdate();
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}
	}

	public void updateTerm(Transaction transaction, TermDto termDto) throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;

		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(UPDATE_ONE);
			prepareStatement.setString(2, termDto.getKey());
			prepareStatement.setString(1, termDto.getValue());
			prepareStatement.executeUpdate();
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}
	}
	private static final String GET_ALL = "select DICTIONARY_KEY, DEFAULT_VALUE from S_DICTIONARY order by DICTIONARY_KEY";
	private static final String INSERT_ONE = "insert into S_DICTIONARY(DICTIONARY_KEY, DEFAULT_VALUE) values (?, ?)";
	private static final String UPDATE_ONE = "update S_DICTIONARY set DEFAULT_VALUE = ? where DICTIONARY_KEY = ? ";
}
